package com.mb.app.appused.base;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.mb.app.appused.base.BindingObject.DataItem;

public abstract class BindingFragment extends Fragment {
	public View baseView = null;
	public LayoutInflater inflater;

	public abstract int getViewLayout();

	public abstract void initView();

	public abstract BindingObject getData();

	List<View> child;
	List<DataItem> list;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		baseView = inflater.inflate(getViewLayout(), null);
		this.inflater = inflater;
		return baseView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		child = getAllChildViews(this.baseView);
		initView();
		invaliMessage(false);
	}

	private InvalUI invalUI;
	public void invaliMessage(Boolean updata) {
		invalUI = new InvalUI(updata);
		invalUI.execute();
	}

	/**
	 * 更新UI
	 * @author xhwwz
	 *
	 */
	class InvalUI extends AsyncTask<Void, Integer, Void> {

		Boolean updata=false;
		
		public InvalUI(Boolean updata){
		   this.updata=updata;	
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			binding(updata);
		}
		

		@Override
		protected void onProgressUpdate(Integer... values) {
			// TODO Auto-generated method stub
			super.onProgressUpdate(values);

		}
	}


	private List<View> getAllChildViews(View view) {
		List<View> allchildren = new ArrayList<View>();
		if (view instanceof ViewGroup) {
			ViewGroup vp = (ViewGroup) view;
			for (int i = 0; i < vp.getChildCount(); i++) {
				View viewchild = vp.getChildAt(i);
				allchildren.add(viewchild);
				allchildren.addAll(getAllChildViews(viewchild));
			}
		}
		return allchildren;
	}

	void binding(Boolean updata) {

		if (!updata)
			list = getData().getList();
		else
			list = getData().regetList();

		if (list == null) {
			return;
		}
		if (child == null) {
			return;
		}
		for (int a = 0; a < child.size(); a++) {
			getViewType(child.get(a));
		}
	}

	void getViewType(View view) {
		if (view == null)
			return;
		Object tag = view.getTag();
		if (tag == null)
			return;
		String key = tag.toString();
		DataItem item = Find(key);

		if (item == null)
			return;

		if (view instanceof Button) {
			((Button) view).setText(item.value);
		}

		if (view instanceof TextView) {
			((TextView) view).setText(item.value);
		}

		if (view instanceof ImageView) {

		}

		if (view instanceof WebView) {
			if (isUrl(item.value))
				((WebView) view).loadUrl(item.value);
		}
	}

	/**
	 * 判断是否为数�?
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		Matcher isNum = pattern.matcher(str);
		if (!isNum.matches()) {
			return false;
		}
		return true;
	}

	/**
	 * 判断为Email
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmail(String str) {
		String regEx = "[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\\.){1,3}[a-zA-z\\-]{1,}";
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(str);
		boolean rs = matcher.matches();
		return rs;
	}

	/**
	 * 判断是否URL
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isUrl(String str) {
		String regEx = "^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$";
		Pattern pattern = Pattern.compile(regEx);
		Matcher matcher = pattern.matcher(str);
		boolean rs = matcher.matches();
		return rs;
	}

	/**
	 * 通过名称读取ID
	 * 
	 * @param name
	 * @return
	 */
	int getId(String name) {
		int idFlag = getResources().getIdentifier(name, "drawable", getActivity().getPackageName());
		return idFlag;
	}

	/**
	 * 获取信息
	 * 
	 * @param key
	 * @return
	 */
	DataItem Find(String key) {
		for (DataItem temp : list) {
			if (key.contains(temp.key))
				return temp;
		}
		return null;
	}

	/**
	 * 获取通用对象
	 * 
	 * @param id
	 * @return
	 */
	protected Object getObject(int id) {
		return getView().findViewById(id);
	}

	public View getView() {
		return baseView;
	}

	public LayoutInflater getInflater() {
		return inflater;
	}
}
